<template>
  <div class="login_container">
    <div class="login_card clearfix">
      <div class="login_img clearfix">
        <img src="../assets/yanlingji.png" alt />
      </div>
      <el-form
        class
        :status-icon="true"
        :model="loginForm"
        ref="loginFormRef"
        :rules="rules"
        label-width="60px"
      >
        <el-form-item label="账号" prop="username">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item class="l login_button">
          <el-button type="primary" size="small" @click="login">登录</el-button>
          <el-button type="primary" size="small" @click="login_reset">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- <input type="text" v-focus> -->
      <div ref="div"></div>
    </div>
  </div>
</template>

<script>
import { login as handlelogin } from '../utils/api'
export default {
  name: 'login',
  data() {
    return {
      a: 'aa',
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 5, max: 7, message: '长度在 5 到 7 个字符', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      // this.$refs.loginFormRef.resetFields()
      console.log('执行mounted里的方法')
    })
  },
  methods: {
    // 登录按钮
    login() {
      this.$refs.loginFormRef.validate((valid) => {
        if (!valid) {
          return
        }
        console.log(111)
        // const { data: res } = await this.$http.post('login', this.loginForm)
        handlelogin(this.loginForm).then((response) => {
          console.log(response, 'res')
          if (response.data.meta.status !== 200) {
            this.$message.warning(response.meta.msg)
          } else {
            window.sessionStorage.setItem('token', response.data.data.token)
            this.$router.push('/home')
          }
        })
        // if (res.meta.status !== 200) {
        //   this.$message({
        //     message: res.meta.msg,
        //     type: 'warning'
        //   })
        // } else {
        //   sessionStorage.setItem('token', res.data.token)
        //   this.$router.push('/home')
        // }
      })
    },
    // 重置表单
    login_reset() {
      // this.$refs.loginFormRef.resetFields()
      console.log(this.$refs.div)
      this.$refs.div.style.width = '10px'
      this.$refs.div.style.height = '10px'
      this.$refs.div.style.backgroundColor = 'red'
      this.loginForm.username = 'zhangsan'
      console.log(this.loginForm.username)
      this.$nextTick(() => {
        setTimeout(() => {
          this.$refs.div.style.width = '20px'
          this.$refs.div.style.height = '20px'
          this.$refs.div.style.backgroundColor = 'yellow'
        }, 0)
      })
    }
  },
  directives: {
    focus: {
      inserted: function (el) {
        el.focus()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.l {
  float: left;
}
.r {
  float: right;
}
.login_container {
  position: relative;
  width: 100%;
  height: 100%;
  background-image: url('../assets/caodong.png');
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
}
.login_card {
  position: absolute;
  width: 400px;
  height: 300px;
  left: 50%;
  transform: translate(-50%, -50%);
  top: 50%;
  border: 1px solid #eee;
  box-shadow: 5px 5px 10px grey;
  background-color: #eee;
  opacity: 0.8;
}
.el-form {
  position: absolute;
  width: 100%;
  bottom: 30px;
}
.login_img {
  position: absolute;
  width: 120px;
  height: 120px;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: pink;
  border: 1px solid #eee;
  padding: 5px;
  border-radius: 50%;
  box-shadow: 0 0 10px #eee;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
  }
}
.login_button {
  margin-left: 20px;
}
.el-input {
  width: 300px !important;
}
.el-button {
  width: 120px;
}
.clearfix:after,
.clearfix:before {
  content: '';
  display: table;
}
.clearfix:after {
  clear: both;
}
.clearfix {
  *zoom: 1;
}
.divnext {
  width: 10px;
  height: 10px;
  background-color: red;
}
</style>>
